package com.zhou.JavaAndroidFirm.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.jaydenxiao.androidfire.bean.VideoData;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.zhou.JavaAndroidFirm.R;
import com.zhou.JavaAndroidFirm.adapter.VideoAdapter;
import com.zhou.JavaAndroidFirm.app.AppConstant;
import com.zhou.JavaAndroidFirm.base.BaseFragment;
import com.zhou.JavaAndroidFirm.constract.VideoListContract;
import com.zhou.JavaAndroidFirm.event.ScollEvent;
import com.zhou.JavaAndroidFirm.model.VideoListModel;
import com.zhou.JavaAndroidFirm.presenter.VideoListPresenter;
import com.zhou.JavaAndroidFirm.widget.LoadingTip;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayer;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayerManager;

/**
 * @author Administrator
 * @date 2018/7/18 0018
 * @des
 */
public class VideosFragment extends BaseFragment<VideoListModel, VideoListPresenter> implements VideoListContract.View, OnRefreshLoadMoreListener, RecyclerView.OnChildAttachStateChangeListener {
    @BindView(R.id.irc)
    RecyclerView irc;
    @BindView(R.id.refresh)
    SmartRefreshLayout refresh;
    @BindView(R.id.loadedTip)
    LoadingTip loadedTip;

    private VideoAdapter mAdapter;
    private List<VideoData> mData = new ArrayList<>();
    private String mVideoType;
    private int mStartPage = 0;

    @Subscribe
    public void scrolltoTop(ScollEvent event) {
        irc.smoothScrollToPosition(0);
    }

    @Override
    protected void lazyFetchData() {
        refresh.autoRefresh();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.framents_news;
    }

    @Override
    protected void initPresenter() {
        mPresenter.onCreate(mModel, this);
    }

    @Override
    protected void initView(Bundle bundle) {
        if (getArguments() != null) {
            mVideoType = getArguments().getString(AppConstant.VIDEO_TYPE);
        }
        mAdapter = new VideoAdapter(mContext, R.layout.item_video_list, mData);
        irc.setLayoutManager(new LinearLayoutManager(mContext));
        irc.setAdapter(mAdapter);
        irc.addOnChildAttachStateChangeListener(this);
        refresh.setOnRefreshLoadMoreListener(this);
    }

    @Override
    protected void initEvent() {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    public void returnVideosListData(List<VideoData> newsSummaries) {
        mAdapter.addAll(newsSummaries);
        setUi(refresh, loadedTip, mAdapter);
    }


    @Override
    public void showTip(String tip) {
        setUi(refresh, loadedTip, mAdapter);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onLoadMore(RefreshLayout refreshLayout) {
        mStartPage += 1;
        //发起请求
        mPresenter.getVideosListData(mVideoType, mStartPage);
    }

    @Override
    public void onRefresh(RefreshLayout refreshLayout) {
        mStartPage += 1;
        mData.clear();
        //发起请求
        mPresenter.getVideosListData(mVideoType, mStartPage);
    }

    @Override
    public void onChildViewAttachedToWindow(View view) {

    }

    @Override
    public void onChildViewDetachedFromWindow(View view) {
        if (JCVideoPlayerManager.listener() != null) {
            JCVideoPlayer videoPlayer = (JCVideoPlayer) JCVideoPlayerManager.listener();
            if (((ViewGroup) view).indexOfChild(videoPlayer) != -1 && videoPlayer.currentState == JCVideoPlayer.CURRENT_STATE_PLAYING) {
                JCVideoPlayer.releaseAllVideos();
            }
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        JCVideoPlayer.releaseAllVideos();
    }
}
